﻿using ProjectCloth.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectCloth.FrmUI.Companys
{
    public partial class CompanyListFrm : Form
    {
        private ClothContext db = new ClothContext();
        //public string CompanyName = "";
        public int CompanyId = 0;
        public CompanyListFrm()
        {
            InitializeComponent();
        }

        private void searchBtn_Click(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;

            dataGridView1.DataSource = (from c in db.Companies.Where(o => o.Enabled == true && (string.IsNullOrEmpty(companyNameTxt.Text) || o.Name.Contains(companyNameTxt.Text)))
                                        select new
                                        {
                                            CompanyName = c.Name,
                                            c.Remark,
                                            c.Id
                                        }).ToList();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                #region  
                DataGridViewColumn column = dataGridView1.Columns[e.ColumnIndex];
                if (column is DataGridViewButtonColumn)
                {
                    var companyId = dataGridView1.CurrentRow.Cells["Id"].Value;
                    if (companyId != null && (int)companyId > 0)
                    {
                        if (column.Name == "EditBtn")
                        {
                            string companyName = dataGridView1.CurrentRow.Cells["CompanyName"].Value.ToString();
                            if (!string.IsNullOrEmpty(companyName))
                            {
                                var company = new EditCompanyFrm((int)companyId);
                                var result = company.ShowDialog();
                                if (result.Equals(DialogResult.Cancel))
                                {
                                    searchBtn_Click(null, null);
                                }


                            }
                        }
                        else if (column.Name == "DeleteBtn")
                        {
                            MessageBoxButtons messButton = MessageBoxButtons.OKCancel;
                            DialogResult dr = MessageBox.Show("确定要删除该供应商吗?", "删除该供应商", messButton);
                            if (dr == DialogResult.OK)//如果点击“确定”按钮
                            {
                                db.Companies.Find((int)companyId).Enabled = false;
                                if (db.SaveChanges() > 0)
                                {
                                    MessageBox.Show("删除成功！");
                                    searchBtn_Click(null, null);
                                }
                                else
                                {
                                    MessageBox.Show("删除失败！");
                                }

                            }

                        }
                    }
                }
                #endregion
            }
        }

        private void AddBtn_Click(object sender, EventArgs e)
        {
            var company = new EditCompanyFrm();
            var result = company.ShowDialog();
            if (result.Equals(DialogResult.Cancel))
            {
                searchBtn_Click(null, null);
            }
        }
    }
}
